package cn.edu.jlu.community.config;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;


class ConfigUtil {
    private static SharedPreferences.Editor editor = null;
    private static SharedPreferences sharedPreferences = null;

    private ConfigUtil() {
        //singleton
    }

    private static SharedPreferences getSharedPreferencesObject(Context context) {
        if (sharedPreferences == null) {
            sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
        }
        return sharedPreferences;
    }

    private static SharedPreferences.Editor getEditorObject(Context context) {
        if (editor == null) {
            editor = getSharedPreferencesObject(context).edit();
        }
        return editor;
    }


    public static int getSharedPreferences(Context context, String key, int def) {
        return getSharedPreferencesObject(context).getInt(key, def);
    }

    public static long getSharedPreferences(Context context, String key, long def) {
        return getSharedPreferencesObject(context).getLong(key, def);
    }

    public static Boolean getSharedPreferences(Context context, String key, Boolean def) {
        return getSharedPreferencesObject(context).getBoolean(key, def);
    }

    public static String getSharedPreferences(Context context, String key, String def) {
        return getSharedPreferencesObject(context).getString(key, def);
    }


    public static boolean setSharedPreferences(Context context, String key, int value) {
        return getEditorObject(context).putInt(key, value).commit();
    }

    public static boolean setSharedPreferences(Context context, String key, long value) {
        return getEditorObject(context).putLong(key, value).commit();
    }

    public static boolean setSharedPreferences(Context context, String key, Boolean value) {
        return getEditorObject(context).putBoolean(key, value).commit();
    }

    public static boolean setSharedPreferences(Context context, String key, String value) {
        return getEditorObject(context).putString(key, value).commit();
    }
}
